package com.freewater.springboot.demo.ioc.eventlistener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;

@Slf4j
public class EventProcessor {

    @EventListener({ContextStartedEvent.class, ContextRefreshedEvent.class})
    public void handleContextStart(Object event) {
        log.info(event.getClass().toString());
    }

    @EventListener(condition = "#root.args[0].level >= 2")
    public void handleCustomEvent(CustomEvent customEvent) {
        log.info(customEvent.toString());
    }

    // 异步执行时间监听
    @EventListener
    @Async
    public void asyncHandleCustomEvent(CustomEvent customEvent) {
        log.info("asyncHandleCustomEvent - {}", customEvent.toString());
    }

    @EventListener
    public void handleEntityCreatedEvent1(EntityCreatedEvent<Person> event) {
        log.info(event.toString());
    }

    @EventListener
    public void handleEntityCreatedEvent2(EntityCreatedEvent<Animal> event) {
        log.info(event.toString());
    }
}
